home *** CD-ROM | disk | FTP | other *** search
/ United Public Domain Gold 2 / United Public Domain Gold 2.iso / utilities / pu247.dms / pu247.adf / Intuition / Gadgets / Example3.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  10KB  |  258 lines

  1. /***********************************************************/
  2. /*                                                         */
  3. /* Amiga C Encyclopedia (ACE) V3.0      Amiga C Club (ACC) */
  4. /* -------------------------------      ------------------ */
  5. /*                                                         */
  6. /* Book:    ACM Intuition               Amiga C Club       */
  7. /* Chapter: Gadgets                     Tulevagen 22       */
  8. /* File:    Example3.c                  181 41  LIDINGO    */
  9. /* Author:  Anders Bjerin               SWEDEN             */
  10. /* Date:    92-05-01                                       */
  11. /* Version: 1.10                                           */
  12. /*                                                         */
  13. /*   Copyright 1992, Anders Bjerin - Amiga C Club (ACC)    */
  14. /*                                                         */
  15. /* Registered members may use this program freely in their */
  16. /*     own commercial/noncommercial programs/articles.     */
  17. /*                                                         */
  18. /***********************************************************/
  19.  
  20. /* This program will open a normal window which is connected to the     */
  21. /* Workbench Screen. The window will use all System Gadgets, and will   */
  22. /* close first when the user has selected the System gadget Close       */
  23. /* window. Inside the window we have put a Boolean gadget with the text */
  24. /* "PRESS ME". The on/off state of the gadget is toggled each time the  */
  25. /* user hitts the gadget.                                               */
  26.  
  27.  
  28.  
  29. #include <intuition/intuition.h>
  30.  
  31.  
  32.  
  33. struct IntuitionBase *IntuitionBase;
  34.  
  35.  
  36.  
  37. /* The coordinates for the box: */
  38. SHORT my_points[]=
  39. {
  40.    0,  0, /* Start at position (0,0) */
  41.   70,  0, /* Draw a line to the right to position (70,0) */
  42.   70, 10, /* Draw a line down to position (70,10) */
  43.    0, 10, /* Draw a line to the right to position (0,10) */
  44.    0,  0  /* Finish off by drawing a line up to position (0,0) */ 
  45. };
  46.  
  47. /* The Border structure: */
  48. struct Border my_border=
  49. {
  50.   0, 0,        /* LeftEdge, TopEdge. */
  51.   1,           /* FrontPen, colour register 1. */
  52.   0,           /* BackPen, for the moment unused. */
  53.   JAM1,        /* DrawMode, draw the lines with colour 1. */
  54.   5,           /* Count, 5 pair of coordinates in the array. */
  55.   my_points,   /* XY, pointer to the array with the coordinates. */
  56.   NULL,        /* NextBorder, no other Border structures are connected. */
  57. };
  58.  
  59.  
  60.  
  61. /* The text string: */
  62. UBYTE my_string[]="PRESS ME";
  63.  
  64. /* The IntuiText structure: */
  65. struct IntuiText my_text=
  66. {
  67.   1,         /* FrontPen, colour register 1. */
  68.   0,         /* BackPen, colour register 0. */
  69.   JAM1,      /* DrawMode, draw the characters with colour 1, do not */
  70.              /* change the background. */ 
  71.   4, 2,      /* LeftEdge, TopEdge. */
  72.   NULL,      /* ITextFont, use default font. */
  73.   my_string, /* IText, the text that will be printed. */
  74.              /* (Remember my_text = &my_text[0].) */
  75.   NULL,      /* NextText, no other IntuiText structures are connected. */
  76. };
  77.  
  78.  
  79.  
  80. struct Gadget my_gadget=
  81. {
  82.   NULL,          /* NextGadget, no more gadgets in the list. */
  83.   40,            /* LeftEdge, 40 pixels out. */
  84.   20,            /* TopEdge, 20 lines down. */
  85.   71,            /* Width, 71 pixels wide. */
  86.   11,            /* Height, 11 pixels lines heigh. */
  87.   GADGHCOMP,     /* Flags, when this gadget is highlighted, the gadget */
  88.                  /* will be rendered in the complement colours. */
  89.                  /* (Colour 0 (00) will be changed to colour 3 (11) */
  90.                  /* (Colour 1 (01)           - " -           2 (10) */
  91.                  /* (Colour 2 (10)           - " -           1 (01) */
  92.                  /* (Colour 3 (11)           - " -           0 (00) */  
  93.   GADGIMMEDIATE| /* Activation, our program will recieve a message when */
  94.                  /* the user has selected this gadget. */
  95.   TOGGLESELECT,  /* The on/off state of the gadget is toggled each time. */
  96.   BOOLGADGET,    /* GadgetType, a Boolean gadget. */
  97.   (APTR) &my_border, /* GadgetRender, a pointer to our Border structure. */
  98.                  /* (Since Intuition does not know if this will be a */
  99.                  /* pointer to a Border structure or an Image structure, */
  100.                  /* Intuition expects an APTR (normal memory pointer). */
  101.                  /* We will therefore have to calm down the compiler by */
  102.                  /* doing some "casting".) */
  103.   NULL,          /* SelectRender, NULL since we do not supply the gadget */
  104.                  /* with an alternative image. (We complement the */
  105.                  /* colours instead) */
  106.   &my_text,      /* GadgetText, a pointer to our IntuiText structure. */
  107.                  /* (See chapter 3 GRAPHICS for more information) */
  108.   NULL,          /* MutualExclude, no mutual exclude. */
  109.   NULL,          /* SpecialInfo, NULL since this is a Boolean gadget. */
  110.                  /* (It is not a Proportional/String or Integer gdget) */
  111.   0,             /* GadgetID, no id. */
  112.   NULL           /* UserData, no user data connected to the gadget. */
  113. };
  114.  
  115.  
  116.  
  117. /* Declare a pointer to a Window structure: */ 
  118. struct Window *my_window;
  119.  
  120. /* Declare and initialize your NewWindow structure: */
  121. struct NewWindow my_new_window=
  122. {
  123.   50,            /* LeftEdge    x position of the window. */
  124.   25,            /* TopEdge     y positio of the window. */
  125.   200,           /* Width       200 pixels wide. */
  126.   100,           /* Height      100 lines high. */
  127.   0,             /* DetailPen   Text should be drawn with colour reg. 0 */
  128.   1,             /* BlockPen    Blocks should be drawn with colour reg. 1 */
  129.   CLOSEWINDOW|   /* IDCMPFlags  The window will give us a message if the */
  130.                  /*             user has selected the Close window gad, */
  131.   GADGETDOWN,    /*             or a gadget has been pressed on. */
  132.   SMART_REFRESH| /* Flags       Intuition should refresh the window. */
  133.   WINDOWCLOSE|   /*             Close Gadget. */
  134.   WINDOWDRAG|    /*             Drag gadget. */
  135.   WINDOWDEPTH|   /*             Depth arrange Gadgets. */
  136.   WINDOWSIZING|  /*             Sizing Gadget. */
  137.   ACTIVATE,      /*             The window should be Active when opened. */
  138.   &my_gadget,    /* FirstGadget A pointer to my_gadget structure. */
  139.   NULL,          /* CheckMark   Use Intuition's default CheckMark. */
  140.   "TOGGLE ME",   /* Title       Title of the window. */
  141.   NULL,          /* Screen      Connected to the Workbench Screen. */
  142.   NULL,          /* BitMap      No Custom BitMap. */
  143.   140,           /* MinWidth    We will not allow the window to become */
  144.   50,            /* MinHeight   smaller than 140 x 50, and not bigger */
  145.   300,           /* MaxWidth    than 300 x 200. */
  146.   200,           /* MaxHeight */
  147.   WBENCHSCREEN   /* Type        Connected to the Workbench Screen. */
  148. };
  149.  
  150.  
  151.  
  152. /*************************************************************************/
  153. /* Extra information:                                                    */
  154. /* You first need to decide what messages the gadgets should report.     */
  155. /* In this case we told the Boolean gadget to send a message if the user */
  156. /* pressed on it. (We sat the flag GADGIMMEDIATE) (The Close window      */
  157. /* gadget will always send a message if someone has selected it.)        */
  158. /* The important thing to remember is that we need to tell the window    */
  159. /* what messages should be allowed to pass by. It was therefore we       */
  160. /* needed to set the IDCMP flags GADGETDOWN and CLOSEWINDOW in the       */
  161. /* NewWindow structure.                                                  */
  162. /*************************************************************************/
  163.  
  164.  
  165.  
  166. main()
  167. {
  168.   /* Boolean variable used for the while loop: */
  169.   BOOL close_me;
  170.  
  171.   /* Declare a variable in which we will store the IDCMP flag: */
  172.   ULONG class;
  173.   
  174.   /* Declare a pointer to an IntuiMessage structure: */
  175.   struct IntuiMessage *my_message;
  176.  
  177.  
  178.  
  179.   /* Before we can use Intuition we need to open the Intuition Library: */
  180.   IntuitionBase = (struct IntuitionBase *)
  181.     OpenLibrary( "intuition.library", 0 );
  182.   
  183.   if( IntuitionBase == NULL )
  184.     exit(); /* Could NOT open the Intuition Library! */
  185.  
  186.  
  187.  
  188.   /* We will now try to open the window: */
  189.   my_window = (struct Window *) OpenWindow( &my_new_window );
  190.   
  191.   /* Have we opened the window succesfully? */
  192.   if(my_window == NULL)
  193.   {
  194.     /* Could NOT open the Window! */
  195.     
  196.     /* Close the Intuition Library since we have opened it: */
  197.     CloseLibrary( IntuitionBase );
  198.  
  199.     exit();  
  200.   }
  201.  
  202.  
  203.  
  204.   /* We have opened the window, and everything seems to be OK. */
  205.  
  206.  
  207.  
  208.   close_me = FALSE;
  209.  
  210.   /* Stay in the while loop until the user has selected the Close window */
  211.   /* gadget: */
  212.   while( close_me == FALSE )
  213.   {
  214.     /* Wait until we have recieved a message: */
  215.     Wait( 1 << my_window->UserPort->mp_SigBit );
  216.  
  217.     /* Collect the message: */
  218.     my_message = (struct IntuiMessage *) GetMsg( my_window->UserPort );
  219.  
  220.     /* Have we collected the message sucessfully? */
  221.     if(my_message)
  222.     {
  223.       /* After we have collected the message we can read it, and save any */
  224.       /* important values which we maybe want to check later: */
  225.       class = my_message->Class;
  226.   
  227.       /* After we have read it we reply as fast as possible: */
  228.       /* REMEMBER! Never try to read a message after you have replied! */
  229.       /* Some other process has maybe changed it. */
  230.       ReplyMsg( my_message );
  231.   
  232.       /* Check which IDCMP flag was sent: */
  233.       switch( class )
  234.       {
  235.         case CLOSEWINDOW:  /* The user selected the Close window gadget! */
  236.                close_me=TRUE;
  237.                break;
  238.                
  239.         case GADGETDOWN:   /* The user has pressed on the Boolean gadget. */
  240.                printf("Hit\n");
  241.                break;
  242.       }
  243.     }
  244.   }  
  245.  
  246.  
  247.  
  248.   /* We should always close the windows we have opened before we leave: */
  249.   CloseWindow( my_window );
  250.  
  251.  
  252.  
  253.   /* Close the Intuition Library since we have opened it: */
  254.   CloseLibrary( IntuitionBase );
  255.   
  256.   /* THE END */
  257. }
  258.